<?php

use Carbon\Carbon;

if (! function_exists('format_percentage')) {
    /**
     * 计算百分比
     * @param $dividend
     * @param $divisor
     * @return string
     */
    function format_percentage($dividend, $divisor) {
        return sprintf("%.2f%%", ($divisor ? $dividend / $divisor : 0) * 100);
    }
}

if (! function_exists('string_to_datetime')) {
    /**
     * Y:m:d H:i:s 字符串转 Carbon
     *
     * @param $datetimeString
     * @return Carbon
     */
    function string_to_datetime($datetimeString) {
        if (Carbon::hasFormat($datetimeString, 'Y-m-d H:i:s')) {
            return Carbon::createFromFormat('Y-m-d H:i:s', $datetimeString);
        } elseif (Carbon::hasFormat($datetimeString, 'Y-m-d H:i')) {
            return Carbon::createFromFormat('Y-m-d H:i', $datetimeString);
        }
    }
}

if (! function_exists('datetime_to_string')) {

    /**
     * @param Carbon $datetime
     * @return string
     */
    function datetime_to_string(Carbon $datetime)
    {
        return $datetime->format('Y-m-d H:i:s');
    }
}

if (! function_exists('format_datetime_with_no_second')) {
    function format_datetime_with_no_second($datetimeString) {
        return Carbon::createFromFormat('Y-m-d H:i:s', $datetimeString)->format('Y-m-d H:i');
    }
}

if (! function_exists('format_datetime_with_second')) {
    function format_datetime_with_second($datetimeString) {
        return Carbon::createFromFormat('Y-m-d H:i', $datetimeString)->format('Y-m-d H:i:s');
    }
}

if (! function_exists('route_prefix_is_admin')) {

    function route_prefix_is_admin() {
        return starts_with(request()->getRequestUri(), '/admin');
    }
}

if (! function_exists('asset_url')) {

    /**
     * @param $path
     * @param string $stylename
     * @return string
     */
    function asset_url($path, $stylename = 'small') {
        return \Illuminate\Support\Facades\Storage::url($path)."?x-oss-process=style/$stylename";
    }
}